package si.cir.gui;

import java.util.ArrayList;
import java.util.List;

import si.cir.R;
import si.cir.entities.UmNovica;
import si.cir.util.WSclient;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class UmNoviceListFragment extends ListFragment {
	
	private List<UmNovica> novice;
	
	public int mesto;
	
	private Runnable getNoviceBackgroundThread = new Runnable() {
		public void run() {
			getNoviceInBackgroundThread();
		}
	};


	
	private void getNoviceInBackgroundThread() {
		novice = WSclient.getUmNovice();
		if(novice != null){
			final List<String> naslovi = new ArrayList<String>();
			for(UmNovica novica : novice)
				naslovi.add(novica.getNaslov());
			getActivity().runOnUiThread(new Runnable() {
		
				public void run() {
					setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.simple_item, naslovi));
					setListShown(true);
				}
			});
		}else{
			getActivity().runOnUiThread(new Runnable() {

				public void run() {
					Toast.makeText(getActivity(), "V tej kategoriji ni objav",
						Toast.LENGTH_LONG).show();
					getActivity().finish();
				}
			});
		}
	}
	
	
	private Runnable getOpisBackgroundThread = new Runnable() {
		public void run() {
			getOpisInBackgroundThread();
		}
	};
	
	private void getOpisInBackgroundThread() {
		novice = WSclient.getUmNovice();
		if(novice != null){
			final List<String> vsebina = new ArrayList<String>();
	
			final UmNovica novica = new UmNovica();
			 novica.opis = novice.get(mesto).getOpis();
			 novica.datum = novice.get(mesto).getDatum();
			 novica.vsebina = novice.get(mesto).getVsebina();
			 
			 vsebina.add(novica.getOpis());
			 vsebina.add(novica.getDatum());
			 vsebina.add(novica.getVsebina());
			
			getActivity().runOnUiThread(new Runnable() {
			
				public void run() {
					setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.simple_item, vsebina));
					setListShown(true);
				}
			});
		}else{
			getActivity().runOnUiThread(new Runnable() {
			
				public void run() {
					Toast.makeText(getActivity(), "V tej kategoriji ni objav",
						Toast.LENGTH_LONG).show();
					getActivity().finish();
				}
			});
		}
	}
	
	
	
	
	

	
	

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		setListShown(false);

		Thread thread = new Thread(null, getNoviceBackgroundThread, "Background");
		thread.start();
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
//		mesto = position;
//		
//		Thread thread = new Thread(null, getOpisBackgroundThread);
//		thread.start();
//		
//		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//		builder.setMessage(novice.get(position).getOpis() + "\n\n" + novice.get(position).getVsebina() + "\n\n" + novice.get(position).getDatum()  )
//		       .setCancelable(true);
//		AlertDialog alert = builder.create();
//		alert.show();

    	Intent intent = new Intent(getActivity(), UmNovicaPodrobnostiActivity.class);
    	intent.putExtra("novica", novice.get(position));
    	startActivity(intent);
	}
}
